package Test;


import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;


public class SimpleFederatedQuery {

final static String serviceEndpoint = "http://sparql.org/sparql";
//final static String serviceEndpoint = "http://data.linkedmdb.org/sparql";

public static void main(String[] args) {

    String queryString1 = 
        "PREFIX imdb: <http://data.linkedmdb.org/resource/movie/> " + 
        "PREFIX dbpedia: <http://dbpedia.org/ontology/> " +
        "PREFIX dcterms: <http://purl.org/dc/terms/> " + 
        "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + 
        "SELECT  * " +  
      "   from <http://www.w3.org/2000/01/rdf-schema#> " +
        "   WHERE { "
            + "{ SELECT ?movie WHERE {" +
        "       SERVICE <http://data.linkedmdb.org/sparql> " + 
        "       { " +  
        "          ?actor1 imdb:actor_name \"Tom Hanks\" . " + 
        "          ?movie imdb:actor ?actor1 ; " +
        "                 dcterms:title ?movieTitle . " + 
        "       } . " +
        "       SERVICE <http://dbpedia.org/sparql> " +
        "       { " + 
        "           ?actor rdfs:label \"Tom Hanks\"@en ; " + 
        "                  dbpedia:birthDate ?birth_date . " +
        "        } " +  
        "   }"
            + " } "
            + " UNION "
               + "{ SELECT ?actor1 WHERE {" +
        "       SERVICE <http://data.linkedmdb.org/sparql> " + 
        "       { " +  
        "          ?actor1 imdb:actor_name \"Tom Hanks\" . " + 
        "          ?movie imdb:actor ?actor1 ; " +
        "                 dcterms:title ?movieTitle . " + 
        "       } . " +
        "       SERVICE <http://dbpedia.org/sparql> " +
        "       { " + 
        "           ?actor rdfs:label \"Tom Hanks\"@en ; " + 
        "                  dbpedia:birthDate ?birth_date . " +
        "        } " +  
        "   }"
            + " } "
  
            
            + " } ";
    String queryString = " SELECT ?oil ?oiq  WHERE { SERVICE <http://foaf/> {<foaf:Toby_Maguire> <foaf:age> <foaf:Toby_Maguire> }  . SERVICE <http://DB1/> {<DB1:Toby_Maguire> <DB1:starring> <DB1:Spiderman> }  BIND ( <DB1:Toby_Maguire> as ?oil )  BIND ( <DB1:Toby_Maguire> as ?oiq ) }";
    

    Query query = QueryFactory.create(queryString1); 
    System.out.println(query);
// exception happens here
    QueryExecution qe = QueryExecutionFactory.sparqlService(serviceEndpoint,query);

    try {
        ResultSet rs = qe.execSelect();
        if ( rs.hasNext() ) {
            // show the result, more can be done here
            System.out.println(ResultSetFormatter.asText(rs));
        }
    } 
    catch(Exception e) { 
        System.out.println(e.getMessage());
    }
    finally {
        qe.close();
    }
    
    System.out.println("\nall done.");
}

}